
Fk:loadTranslationTable{
  ["hanqing_3"] = "彪炳千秋",
}

local godlikui = General(extension, "godlikui", "god", 3)
local Wanxiang = function (player, general, skillName)
  local room = player.room
  local suit = room:askForChoice(player, {"log_spade", "log_heart", "log_club", "log_diamond"}, skillName,
    "#hanqing__wangxiang-suit:::"..general)
  local choices = {}
  for i = 1, 13, 1 do
    table.insert(choices, tostring(i))
  end
  local num = room:askForChoice(player, choices, skillName, "#hanqing__wangxiang-num:::"..general)
  choices = {}
  for _, s in ipairs(Fk.generals[general]:getSkillNameList(true)) do
    local skill = Fk.skills[s]
    if skill.frequency ~= Skill.Compulsory then
      table.insert(choices, s)
    end
  end
  local card = room:printCard("wangxiang_general", U.ConvertSuit(suit, "sym", "int"), tonumber(num))
  room:setCardMark(card, "@hanqing__wangxiang_general", Util.TranslateMapper(general))
  room:setCardMark(card, "hanqing__wangxiang_general", general)
  if #choices > 0 then
    local skill = room:askForChoice(player, choices, skillName, "#hanqing__wangxiang-skill:::"..general)
    room:setCardMark(card, "@hanqing__wangxiang_skill", Util.TranslateMapper(skill))
    room:setCardMark(card, "hanqing__wangxiang_skill", skill)
  end
  return card
end
local wangxiang = fk.CreateTriggerSkill{
  name = "hanqing__wangxiang",
  anim_type = "special",
  frequency = Skill.Compulsory,
  events = {fk.GameStart, fk.RoundStart, fk.RoundEnd},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      if event == fk.GameStart then
        return #player.room.general_pile > 0
      elseif event == fk.RoundStart then
        return #player:getTableMark("@&hanqing__wangxiang") > 0
      elseif event == fk.RoundEnd then
        return player.deputyGeneral ~= ""
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.GameStart then
      local generals = table.random(room.general_pile, 2 * room.settings.generalNum)
      local result = room:askForCustomDialog(player, self.name, "packages/utility/qml/ChooseGeneralsAndChoiceBox.qml", {
        generals,
        {"OK"},
        "#hanqing__wangxiang-choice",
        {},
        1,
        4,
        {},
      })
      local choice = {}
      if result ~= "" then
        local reply = json.decode(result)
        if reply.choice == "OK" then
          choice = reply.cards
          for _, name in ipairs(choice) do
            room:findGeneral(name)
          end
        end
      end
      if #choice < 4 then
        table.insertTable(choice, table.random(room.general_pile, 4 - #choice))
      end
      for _, name in ipairs(choice) do
        room:findGeneral(name)
      end
      room:setPlayerMark(player, "@&hanqing__wangxiang", choice)
    elseif event == fk.RoundStart then
      local generals = player:getTableMark("@&hanqing__wangxiang")
      local skills = {}
      for _, general in ipairs(generals) do
        table.insert(skills, Fk.generals[general]:getSkillNameList())
      end
      local result = room:askForCustomDialog(player, self.name,
        "packages/utility/qml/ChooseSkillFromGeneralBox.qml",
        {
          generals,
          skills,
          "#hanqing__wangxiang-deputy",
        }
      )
      if result == "" then
        return
      else
        result = json.decode(result)
      end
      local general, _ = table.unpack(result)
      room:changeHero(player, general, false, true, true, false, false)
    elseif event == fk.RoundEnd then
      room:removeTableMark(player, "@&hanqing__wangxiang", player.deputyGeneral)
      local card = Wanxiang(player, player.deputyGeneral, self.name)
      room:changeHero(player, "", false, true, true, false, false)
      room:moveCards({
        ids = {card.id},
        toArea = Card.DrawPile,
        moveReason = fk.ReasonJustMove,
        skillName = self.name,
        drawPilePosition = math.random(#room.draw_pile),
      })
      if #player:getTableMark("@&hanqing__wangxiang") == 0 and not player.dead then
        room.logic:trigger("fk.hanqing__jugu", player, {}, false)
      end
    end
  end,

  on_lose = function (self, player, is_death)
    player.room:setPlayerMark(player, "@&hanqing__wangxiang", 0)
  end,
}
local jugu = fk.CreateTriggerSkill{
  name = "hanqing__jugu",
  anim_type = "big",
  frequency = Skill.Compulsory,
  events = {"fk.hanqing__jugu"},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self)
  end,
  on_use = function(self, event, target, player, data)
    player:gainAnExtraTurn(true, self.name)
  end,
}
local jugu_delay = fk.CreateTriggerSkill{
  name = "#hanqing__jugu_delay",
  mute = true,
  events = {fk.DrawNCards},
  can_trigger = function (self, event, target, player, data)
    return target == player and player:usedSkillTimes("hanqing__jugu", Player.HistoryGame) > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    data.n = 0
    local cards = room:getCardsFromPileByRule("wangxiang_general", 2, "drawPile")
    if #cards < 2 then
      table.insertTable(cards, room:getCardsFromPileByRule("wangxiang_general", 2 - #cards, "discardPile"))
    end
    if #cards > 0 then
      room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonJustMove, "hanqing__jugu", nil, false, player.id)
    end
  end,
}
local qielv = fk.CreateActiveSkill{
  name = "hanqing__qielv",
  anim_type = "offensive",
  min_card_num = 1,
  target_num = 1,
  prompt = "#hanqing__qielv",
  interaction = function(self)
    local choices = {}
    for _, id in ipairs(Self:getCardIds("h&")) do
      local card = Fk:getCardById(id)
      if card.name == "wangxiang_general" and card:getMark("hanqing__wangxiang_general") ~= 0 then
        table.insert(choices, card:getMark("hanqing__wangxiang_general"))
      end
    end
    if not table.contains({"", "blank_shibing", "blank_nvshibing"}, Self.general) then
      table.insert(choices, Self.general)
    end
    if not table.contains({"", "blank_shibing", "blank_nvshibing"}, Self.deputyGeneral) then
      table.insert(choices, Self.deputyGeneral)
    end
    table.insertTableIfNeed(choices, Self:getTableMark("@&hanqing__wangxiang"))
    if #choices == 0 then return end
    return UI.ComboBox { choices = choices }
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryRound) == 0
  end,
  card_filter = function (self, player, to_select, selected)
    if self.interaction.data then
      if Fk:getCardById(to_select):getMark("hanqing__wangxiang_general") == self.interaction.data then  --不能置顶即将使用的武将牌
        return false
      end
      if table.find(Self:getPile("$role_carriage&"), function (id)
        local c = Fk:getCardById(id)
        return c.name == "wangxiang_general" and c:getMark("hanqing__wangxiang_general") == self.interaction.data
      end) then
        return Fk:getCardById(to_select).name ~= "role__wooden_ox"  --不能同时将木马置顶并使用其中的武将牌
      end
      return true
    end
  end,
  target_filter = function(self, player, to_select, selected)
    return #selected == 0 and to_select ~= Self.id
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local card
    local from_hand = true
    if self.interaction.data == player.general then
      from_hand = false
      card = Wanxiang(player, self.interaction.data, self.name)
      room:changeHero(player, player:isMale() and "blank_shibing" or "blank_nvshibing", false, false, true, false, false)
    elseif self.interaction.data == player.deputyGeneral then
      from_hand = false
      card = Wanxiang(player, self.interaction.data, self.name)
      room:changeHero(player, "", false, true, true, false, false)
    end
    if table.contains(player:getTableMark("@&hanqing__wangxiang"), self.interaction.data) then
      if from_hand then
        from_hand = false
        card = Wanxiang(player, self.interaction.data, self.name)
      end
      room:removeTableMark(player, "@&hanqing__wangxiang", self.interaction.data)
      if #player:getTableMark("@&hanqing__wangxiang") == 0 then
        room.logic:trigger("fk.hanqing__jugu", player, {}, false)
      end
    end
    local moves = {{
      ids = effect.cards,
      from = player.id,
      toArea = Card.DrawPile,
      moveReason = fk.ReasonJustMove,
      skillName = self.name,
      proposer = player.id,
      drawPilePosition = 1,
    }}
    if from_hand then
      card = table.find(player:getCardIds("h&"), function (id)
        local c = Fk:getCardById(id)
        return c.name == "wangxiang_general" and c:getMark("hanqing__wangxiang_general") == self.interaction.data
      end)
      if card then
        card = Fk:getCardById(card)
        table.insert(moves, {
          ids = {card.id},
          from = player.id,
          toArea = Card.Processing,
          moveReason = fk.ReasonUse,
        })
      end
    else
      table.insert(moves, {
        ids = {card.id},
        toArea = Card.Processing,
        moveReason = fk.ReasonUse,
      })
    end
    if card then
      room:sendLog{
        type = "#UseCardToTargets",
        from = player.id,
        to = { target.id },
        card = { card.id },
      }
    end
    room:moveCards(table.unpack(moves))
    local data = {
      who = target,
      reason = self.name,
      pattern = ".",
      extra_data = {hanqing__qielvSource = effect.from}
    }
    room:judge(data)
    local result, str = {}, ""
    if data.card.name == "wangxiang_general" then
      if data.card:getMark("hanqing__wangxiang_skill") ~= 0 then
        str = Fk:translate(":"..data.card:getMark("hanqing__wangxiang_skill"), "zh_CN")
      end
    else
      str = Fk:translate(":"..data.card.name, "zh_CN")
    end
    if string.find(str, "体力") then
      table.insert(result, "@@hanqing__qielv_chi")
    end
    if string.find(str, "武器") then
      table.insert(result, "@@hanqing__qielv_zhang")
    end
    if string.find(str, "打出") then
      table.insert(result, "@@hanqing__qielv_tu")
    end
    if string.find(str, "距离") then
      table.insert(result, "@@hanqing__qielv_liu")
    end
    if #result == 0 then
      table.insert(result, "@hanqing__qielv_si")
    end
    if result[1] ~= "@hanqing__qielv_si" then
      for _, mark in ipairs({"@@hanqing__qielv_chi", "@@hanqing__qielv_zhang", "@@hanqing__qielv_tu", "@@hanqing__qielv_liu"}) do
        room:setPlayerMark(data.who, mark, 0)
      end
    end
    for _, mark in ipairs(result) do
      room:addPlayerMark(data.who, mark, 1)
      if mark == "@hanqing__qielv_si" and not data.who:isNude() and not player.dead then
        local c = room:askForCardChosen(player, target, "he", "hanqing__qielv")
        room:obtainCard(player.id, c, false, fk.ReasonPrey)
      end
    end
    if card then
      room:moveCards({
        ids = {card.id},
        toArea = Card.DiscardPile,
        moveReason = fk.ReasonUse,
      })
    end
  end,
}
local qielv_delay = fk.CreateTriggerSkill{
  name = "#hanqing__qielv_delay",
  mute = true,
  events = {fk.FinishJudge, fk.Damaged, fk.TargetConfirmed, fk.AfterCardsMove, fk.EventPhaseStart, fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    if player.dead then return false end
    if event == fk.FinishJudge then
      return data.extra_data and data.extra_data.hanqing__qielvSource == player.id and
        player.room:getCardArea(data.card) == Card.Processing
    elseif event == fk.Damaged then
      return target == player and player:getMark("@@hanqing__qielv_chi") > 0
    elseif event == fk.TargetConfirmed then
      return target == player and data.card.trueName == "slash" and player:getMark("@@hanqing__qielv_zhang") > 0
    elseif event == fk.AfterCardsMove and player:getMark("@@hanqing__qielv_tu") > 0 and not player:isKongcheng() then
      for _, move in ipairs(data) do
        if move.skillName ~= "hanqing__qielv" and move.from == player.id then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand then
              return true
            end
          end
        end
      end
    elseif event == fk.EventPhaseStart then
      return target == player and player:getMark("@@hanqing__qielv_liu") > 0 and player.phase == Player.Finish
    elseif event == fk.TurnEnd then
      return target == player and player:getMark("@hanqing__qielv_si") > #player.room.alive_players
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.FinishJudge then
      if room:getCardArea(data.card) == Card.Processing then
        room:obtainCard(player.id, data.card, true, fk.ReasonJustMove)
      end
      return false
    end
    room:notifySkillInvoked(player, "hanqing__qielv", "negative")
    player:broadcastSkillInvoke("hanqing__qielv")
    if event == fk.Damaged then
      room:loseHp(player, data.damage, "hanqing__qielv")
    elseif event == fk.TargetConfirmed then
      data.disresponsive = true
    elseif event == fk.AfterCardsMove then
      local cards = table.filter(player.player_cards[Player.Hand], function (id)
        return not player:prohibitDiscard(Fk:getCardById(id))
      end)
      if #cards > 0 then
        room:throwCard(table.random(cards, 1), "hanqing__qielv", player, player)
      end
    elseif event == fk.EventPhaseStart then
      player:turnOver()
    elseif event == fk.TurnEnd then
      room:killPlayer({who = player.id})
    end
  end,
}
local qielv_maxcards = fk.CreateMaxCardsSkill {
  name = "#hanqing__qielv_maxcards",
  correct_func = function(self, player)
    return -player:getMark("@hanqing__qielv_si")
  end,
}
jugu:addRelatedSkill(jugu_delay)
qielv:addRelatedSkill(qielv_delay)
qielv:addRelatedSkill(qielv_maxcards)
godlikui:addSkill(wangxiang)
godlikui:addSkill(jugu)
godlikui:addSkill(qielv)
AddWinAudio(godlikui)
Fk:loadTranslationTable{
  ["godlikui"] = "神李悝",
  ["#godlikui"] = "",
  ["illustrator:godlikui"] = "有硬币有果",
  ["designer:godlikui"] = "苍淇",
  ["cv:godlikui"] = "妙啊",

  ["hanqing__wangxiang"] = "万象",
  [":hanqing__wangxiang"] = "锁定技，游戏开始时，你将四张备选武将牌置于你武将牌上（从剩余武将牌堆中任意选择）。每轮开始时，你挑选一张"..
  "作为你本轮的副将；每轮结束时，你<a href='hanqing__wangxiangPut'>将副将洗入牌堆</a>。",
  ["hanqing__jugu"] = "矩古",
  [":hanqing__jugu"] = "锁定技，你失去所有“万象”牌后，你于当前回合结束后获得一个额外回合，然后你的摸牌阶段均改为从牌堆或弃牌堆中检索两张“万象”牌。",
  ["hanqing__qielv"] = "挈律",
  [":hanqing__qielv"] = "每轮限一次，你可以将至少一张牌置于牌堆顶，<a href='hanqing__wangxiangPut'>将一张武将牌转化</a>为一次“神裁”。",
  ["@&hanqing__wangxiang"] = "万象",
  ["#hanqing__wangxiang-choice"] = "万象：请选择四张备选武将牌",
  ["#hanqing__wangxiang-deputy"] = "万象：选择一名武将作为你本轮的副将",
  ["#hanqing__wangxiang-suit"] = "将你的武将牌【%arg】洗入牌堆，请声明其花色",
  ["#hanqing__wangxiang-num"] = "将你的武将牌【%arg】洗入牌堆，请声明其点数",
  ["#hanqing__wangxiang-skill"] = "将你的武将牌【%arg】洗入牌堆，请声明其技能",
  ["@hanqing__wangxiang_skill"] = "",
  ["@hanqing__wangxiang_general"] = "",
  ["hanqing__wangxiangPut"] = "当将一张武将牌加入游戏牌堆（包括牌堆、弃牌堆等任意游戏牌区域）时，须声明此牌的花色、点数，并声明此武将牌上的一个"..
  "非锁定技作为此牌的效果。",
  ["#hanqing__qielv"] = "挈律：将至少一张牌置于牌堆顶，选择一张武将牌转化为“神裁”！",
  ["@@hanqing__qielv_chi"] = "笞",
  ["@@hanqing__qielv_zhang"] = "杖",
  ["@@hanqing__qielv_tu"] = "徒",
  ["@@hanqing__qielv_liu"] = "流",
  ["@hanqing__qielv_si"] = "死",

  ["$hanqing__wangxiang1"] = "冲漠无我，不置私家，万象森然已具！",
  ["$hanqing__wangxiang2"] = "百川异源皆归于海，百家殊业皆务于治！",
  ["$hanqing__jugu1"] = "苟利国家不必法古，苟周于事不必循旧！",
  ["$hanqing__jugu2"] = "法度制令顺时而变，因俗守旧难当世事！",
  ["$hanqing__qielv1"] = "窥宫者膑，拾遗者刖，杀人者诛！",
  ["$hanqing__qielv2"] = "不别亲疏，不殊贵贱，一断于法！",
  ["$godlikui_win_audio"] = "执法而定，律吕和谐，是以别春秋，协万里！",
  ["~godlikui"] = "沉酣入骨，血犹可舍，腹心难净！",
}

return extension
